//
//  EditOilRecordViewController.m
//  carcareIOS
//
//  Created by wr on 16/4/11.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "OilDetailViewController.h"
#import "AlertActionSheetMgr.h"
//#import "OilMapDetailViewController.h"
#import "FuelMgr.h"
#import "CalendarMgr.h"

#define CELL_HEIGHT_BIG (70)
#define CELL_HEIGHT (50)
#define SECTION_HEIGHT (12)
#define SECTION_HEIGHT_WORD (44)
#define CELL_HEIGHT_BTN (37 + CELL_HEIGHT)
#define PICKER_VIEW_HEIGHT (261)
typedef NS_ENUM(NSInteger , View_Type) {
    View_Type_TF = 10,//cell 的输入框的tag
    View_Type_Delete_Btn ,//cell上面的删除按钮
    View_Type_LeftL,//cell左侧的label
};

@interface OilDetailViewController ()< UIPickerViewDataSource , UIPickerViewDelegate>

@property (nonatomic , readonly) NSArray * titleArray;

//编辑或保存按钮 控制 默认为NO 当点击编辑后  变为 YES  点击保存 变为NO
//@property (nonatomic , assign) BOOL EditOrSaveControl;

//PickView
@property (nonatomic , strong) UIView * oilPickerBackView;
@property (nonatomic , strong) UIPickerView * oilPickerView;
@property (nonatomic , copy) NSArray * oilDataPickArr;
@property (nonatomic , strong) NSDictionary * oilDataPickDic;


@property (nonatomic , strong) UITextField * fuelCountPriceTF;//加油金额
@property (nonatomic , strong) UITextField * fuelNumTF;//汽油标号
@property (nonatomic , strong) UITextField * fuelUnitPriceTF;//汽油单价
@property (nonatomic , strong) UITextField * fuelOilTF;//加油升数

@property (nonatomic , strong) UITextField * fuelTimeMileTF;//加时里程
@property (nonatomic , strong) UITextField * fuelTimeTF;// 加油时间
@property (nonatomic , strong) UITextField * fuelPlaceTF;//加油站

//日历
//@property (nonatomic, strong) CalendarView *calendarView;
//@property (nonatomic, copy) NSString *choiceDate;
@property (nonatomic , strong) UIView * datePickBackView;
@property (nonatomic , strong) UIPickerView * datePickView;
@property (nonatomic , copy) NSArray * datePickerHournArr;

@property (nonatomic , strong) FuelRecordModel * model;

@property (nonatomic , assign) VC_Type vcType;

@property (nonatomic, strong) NSMutableArray *pickDates;//可选取的数据

@end

@implementation OilDetailViewController
- (instancetype)initWithInputType:(TYPE_INPUT)type withVCType:(VC_Type)vcType
{
    self = [super initWithInputType:type];
    if (self) {
        self.vcType = vcType;
        if([FuelMgr sharedInstance].choiceRecordModel)self.model = [FuelMgr sharedInstance].choiceRecordModel;
        //
        
        self.pickDates = [NSMutableArray arrayWithCapacity:50];
        [self addPickDates];
        
    }
    return self;
}

-(void)addPickDates{
    
//    NSString *format = @"yyyy-MM-dd (ccc)";
//    NSDate *date = [NSDate date];
//    if (self.pickDates.count > 0) {
//        date = [CALENDARMGR dateWithStr:self.pickDates.lastObject format:format];
//        NSString *str = [CALENDARMGR dateStrWithDate:date format:yyyy_MM_dd];
//        if ([str isEqualToString:USERINFO.bindingDay.dateStr]){
//
//        }
//    }
//    for (int i = 0; i < 50; i++) {
//        [self.pickDates addObject:[CALENDARMGR dateStrWithDate:[CALENDARMGR dateFromDate:[NSDate date] beforeDays:i + j] format:@"yyyy-MM-dd (ccc)"]];
//    }
    
    NSInteger j = self.pickDates.count;
    for (int i = 0; i < 50; i++) {
        [self.pickDates addObject:[CALENDARMGR dateStrWithDate:[CalendarMgr dateFromDate:[NSDate date] beforeDays:i + j] format:@"yyyy-MM-dd (ccc)"]];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = (self.vcType == VC_Type_CanEdit_Add ? @"新增加油记录" :@"加油明细");
    
//    [self.calendarView showCurrentMonth];
   
    if (self.vcType == VC_Type_CanEdit_Add || self.vcType == VC_Type_CanEdit_Map || self.vcType == VC_Type_CanEdit_NoMap) {
        __weak __typeof(self) wself = self;
        [self navSetupLeftImageName:@"close_icon" action:^{
            [wself dismissViewControllerAnimated:YES completion:nil];
        }];
        [self setUpRightNavSaveBtn];
    }else if(self.vcType == VC_Type_Look)
    {
        [self setUpRightNavEditBtn];
    }
    
    
    [self createTableView];
    
    self.oilDataPickArr = @[@"京",@"沪",@"苏",@"粤",@"国"];
    self.oilDataPickDic = @{@"京":@[@"89#",@"92#",@"95#",@"0#"],@"沪":@[@"89#",@"92#",@"95#",@"0#"],@"苏":@[@"89#",@"92#",@"95#",@"0#"],@"粤":@[@"89#",@"92#",@"95#",@"0#"],@"国":@[@"90#",@"93#",@"97#",@"0#"]};
    NSMutableArray *tmp = [NSMutableArray arrayWithCapacity:24];
    for (int i = 0; i < 24; i++) {
        [tmp addObject:[NSString stringWithFormat:@"%zd点",i]];
    }
    self.datePickerHournArr =tmp;
    [self createDatePickView];
    [self createPickView];
    

}
#pragma makr - 创建日历pickview
- (void)createDatePickView
{
    self.datePickBackView = [[UIView viewWithFrame:CGRectMake(0, __MAIN_HEIGHT_64 - PICKER_VIEW_HEIGHT, __MAIN_WIDTH, PICKER_VIEW_HEIGHT)] resetBackgroundColor:[UIColor whiteColor]];
    
    CGFloat headViewH = 45;
    UIView * smallHeadView = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, headViewH)] resetBackgroundColor:[Theme backGroundColor]] setupOnView:self.datePickBackView];
    smallHeadView.layer.borderColor = [Theme lineColor].CGColor;
    smallHeadView.layer.borderWidth = 0.5;
    [Factory labelWithFrame:CGRectMake(0, 0, smallHeadView.frame.size.width, smallHeadView.frame.size.height) font:FONT_DEFAULT_Light(17) text:@"选择时间" textColor:COLOR_HEXSTRING(@"#424242") onView:smallHeadView textAlignment:NSTextAlignmentCenter];
    CGFloat buttonW = 60;
    [[[UIButton buttonWithFrame:CGRectMake(0, 0, buttonW, headViewH) title:@"取消" textColor:COLOR_HEXSTRING(@"#2090e6") font:FONT_BUTTON click:^{
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
    }] resetBackgroundColor:[UIColor clearColor]] setupOnView:smallHeadView];
    
    __weak __typeof(self) wself = self;

    [[[UIButton buttonWithFrame:CGRectMake(smallHeadView.frame.size.width - buttonW, 0, buttonW, headViewH ) title:@"确认" textColor:COLOR_HEXSTRING(@"#2090e6") font:FONT_BUTTON click:^{
        NSInteger selectOneRow = [wself.datePickView selectedRowInComponent:0];
        NSInteger selectSecondRow = [wself.datePickView selectedRowInComponent:1];
        NSString *choiceDate = [NSString stringWithFormat:@"%@  %@",wself.pickDates[selectOneRow], wself.datePickerHournArr[selectSecondRow]];
        UITableViewCell * cell = [wself.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:3]];
        UITextField * textFD = (UITextField *)[cell viewWithTag:View_Type_TF];
        textFD.text = choiceDate;
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
    }] resetBackgroundColor:[UIColor clearColor]] setupOnView:smallHeadView];

    
    self.datePickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, headViewH , __MAIN_WIDTH, PICKER_VIEW_HEIGHT - headViewH)];
    self.datePickView.dataSource = self;
    self.datePickView.delegate = self;
    self.datePickView.backgroundColor = [UIColor clearColor];
    [self.datePickBackView addSubview:self.datePickView];
    
}
#pragma mark -  pickerview 汽油标号
- (void)createPickView
{
    self.oilPickerBackView = [[UIView viewWithFrame:CGRectMake(0, __MAIN_HEIGHT_64 - PICKER_VIEW_HEIGHT, __MAIN_WIDTH, PICKER_VIEW_HEIGHT)] resetBackgroundColor:[UIColor whiteColor]];
    
    CGFloat headViewH = 45;
    UIView * smallHeadView = [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, headViewH)] resetBackgroundColor:[Theme backGroundColor]] setupOnView:self.oilPickerBackView];
    smallHeadView.layer.borderColor = [Theme lineColor].CGColor;
    smallHeadView.layer.borderWidth = 0.5;
    [Factory labelWithFrame:CGRectMake(0, 0, smallHeadView.frame.size.width, smallHeadView.frame.size.height) font:FONT_DEFAULT_Light(17) text:@"选择汽油标号" textColor:COLOR_HEXSTRING(@"#424242") onView:smallHeadView textAlignment:NSTextAlignmentCenter];
    CGFloat buttonW = 60;
    [[[UIButton buttonWithFrame:CGRectMake(0, 0, buttonW, headViewH) title:@"取消" textColor:COLOR_HEXSTRING(@"#2090e6") font:FONT_BUTTON click:^{
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
    }] resetBackgroundColor:[UIColor clearColor]] setupOnView:smallHeadView];
    
    __weak __typeof(self) wself = self;
    [[UIButton buttonWithFrame:CGRectMake(smallHeadView.frame.size.width - buttonW, 0, buttonW, headViewH ) title:@"确认" textColor:COLOR_HEXSTRING(@"#2090e6") font:FONT_BUTTON click:^{
        NSInteger selectOneRow = [wself.oilPickerView selectedRowInComponent:0];
        NSInteger selectSecondRow = [wself.oilPickerView selectedRowInComponent:1];
        NSString *choiceFuelNO = [NSString stringWithFormat:@"%@%@",wself.oilDataPickArr[selectOneRow], wself.oilDataPickDic[wself.oilDataPickArr[selectOneRow]][selectSecondRow]];
        UITableViewCell * cell = [wself.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
        UITextField * textFD = (UITextField *)[cell viewWithTag:View_Type_TF];
        textFD.text = choiceFuelNO;
        [AlertMgr dismiss:TYPE_VIEW_POPUP];
    }] setupOnView:smallHeadView];
    
    self.oilPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, headViewH , __MAIN_WIDTH, PICKER_VIEW_HEIGHT - headViewH) ];
    self.oilPickerView.dataSource = self;
    self.oilPickerView.delegate = self;
    self.oilPickerView.backgroundColor = [UIColor clearColor];
    [self.oilPickerBackView addSubview:self.oilPickerView];
    

}
#pragma mark - pickview Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(pickerView == self.oilPickerView)
    {
        if (component == 0) {
            
            return self.oilDataPickArr.count;
        }
        else
        {
            if (self.oilDataPickArr.count == 0) {
                
                return 0;
            }else
            {
                NSInteger selectOneRow = [self.oilPickerView selectedRowInComponent:0];
                
                return [self.oilDataPickDic[self.oilDataPickArr[selectOneRow]] count];
            }
        }
    }else
    {
        if (component == 0) {
            
            return self.pickDates.count;
        }
        else
        {
            if (self.pickDates.count == 0) {
                
                return 0;
            }else
            {
                
                return [self.datePickerHournArr count];
            }
        }
    }
    
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if(pickerView == self.oilPickerView)
    {
        if (component == 0) {
            
            return __MAIN_WIDTH / 2;
        }else
        {
            
            return __MAIN_WIDTH / 2;
        }
        
    }else
    {
        if (component == 0) {
            
            return __MAIN_WIDTH / 10 * 7;
        }else
        {
            return __MAIN_WIDTH / 10 * 3;
        }

    }
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(pickerView == self.oilPickerView)
    {
        if (component == 0) {
            
            return self.oilDataPickArr[row];
        }else
        {
            
            NSInteger selectOneRow = [self.oilPickerView selectedRowInComponent:0];
            return self.oilDataPickDic[self.oilDataPickArr[selectOneRow]][row];
        }

    }else
    {
        if (component == 0) {
//            NSInteger selectOneRow = [self.datePickView selectedRowInComponent:0];
            
            if(row == self.pickDates.count - 49 || row == self.pickDates.count - 19)
            {
                [self addPickDates];
                [self.datePickView reloadAllComponents];
                
            }
        
            return self.pickDates[row];
        }else
        {

            return self.datePickerHournArr[row];
        }
    }
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView == self.oilPickerView)
    {
        if (component == 0) {
            
            [self.oilPickerView reloadComponent:1];
        }

    }else
    {
        if (component == 0) {
            [self.datePickView selectRow:0 inComponent:1 animated:YES];
            [self.datePickView reloadComponent:1];
        }
    }
}

- (void)setUpRightNavSaveBtn
{
    __weak __typeof(self) wself = self;
    [self navSetupRightTitle:@"保存" action:^{
        [wself clickOnBG];

        [wself addSaveBtnClick];
        [wself.tableView reloadData];
    }];
}
- (void)setUpRightNavEditBtn
{
    __weak __typeof(self) wself = self;
    [self navSetupRightTitle:@"编辑" action:^{
        [FuelMgr sharedInstance].choiceRecordModel = wself.model;
        [wself presentViewController:[UINavigationController navigationControllerWithRootViewController:[[OilDetailViewController alloc] initWithInputType:TYPE_INPUT_TABLEVIEW withVCType:VC_Type_CanEdit_NoMap]] animated:YES completion:nil];

        [wself.tableView reloadData];
    }];
}
#pragma mark - 保存按钮事件
- (void)addSaveBtnClick
{
    [self clickOnBG];
    NSString * errMessage;
    if(![self.fuelCountPriceTF.text isNotNil])
    {
        errMessage = self.fuelCountPriceTF.placeholder;
    }else if (![self.fuelNumTF.text isNotNil])
    {
        errMessage = self.fuelNumTF.placeholder;
    }else if(![self.fuelUnitPriceTF.text isNotNil])
    {
        errMessage =  self.fuelUnitPriceTF.placeholder;
    }
    if([self.fuelCountPriceTF.text isNotNil] && [self.fuelCountPriceTF.text integerValue] == 0)
    {
        errMessage = @"加油金额不能为0";
    }
    if([self.fuelUnitPriceTF.text isNotNil] && [self.fuelUnitPriceTF.text integerValue] == 0)
    {
        errMessage = @"汽油单价不能为0";
    }
    
    if ([errMessage isNotNil]) {
        [AlertMgr showShortWarnTextTips:@[errMessage]];
    }else
    {
//        [self.fuelCountPriceTF.text floatValue]
        if(self.vcType == VC_Type_CanEdit_NoMap || self.vcType == VC_Type_CanEdit_Map)
        {
            [self updateDataClick];
        }else if(self.vcType == VC_Type_CanEdit_Add)
        {
            [self addDataClick];
        }

    }
    
}
#pragma mark - 更新保存按钮数据
- (void)updateDataClick
{
    self.model.totalPrice = [self.fuelCountPriceTF.text floatValue];
    self.model.NEWFUELLABEL = self.fuelNumTF.text;
    self.model.FUELPRICE = [self.fuelUnitPriceTF.text floatValue];
    self.model.fuelVol = [self.fuelOilTF.text floatValue];
    
    self.model.NEWRECORDS = ([self.fuelTimeMileTF.text isNotNil] ? [self.fuelTimeMileTF.text integerValue] : 0);
    self.model.cdt = ([self.fuelTimeTF.text isNotNil] ? self.fuelTimeTF.text : @"");
    self.model.GSTATION_NAME = ([self.fuelPlaceTF.text isNotNil] ? self.fuelPlaceTF.text : @"");
    
    [FuelMgr sharedInstance].choiceRecordModel = self.model;
    __weak __typeof(self) wself = self;
    [[FuelMgr sharedInstance]updateFuelRecord:self.model succ:^{
        [[StatisticsMgr sharedInstance] addClickID:@"加油记录－保存修改加油记录按钮" description:@""];
        [AlertMgr showShortSuccessTextTips:@[@"保存数据成功"]];
        [wself dismissViewControllerAnimated:YES completion:nil];
    } fail:^{
        
    }];
}
#pragma mark - 新增保存按钮数据
- (void)addDataClick
{
    FuelRecordModel * model = [[FuelRecordModel alloc]init];
    model.totalPrice = [self.fuelCountPriceTF.text floatValue];
    model.NEWFUELLABEL = self.fuelNumTF.text;
    model.FUELPRICE = [self.fuelUnitPriceTF.text floatValue];
    model.fuelVol = [self.fuelOilTF.text floatValue];
    
    model.cdt = ([self.fuelTimeTF.text isNotNil] ? self.fuelTimeTF.text : @"");
    model.NEWRECORDS = ([self.fuelTimeMileTF.text isNotNil] ? [self.fuelTimeMileTF.text integerValue] : 0);
    model.GSTATION_NAME = ([self.fuelPlaceTF.text isNotNil] ? self.fuelPlaceTF.text : @"");
    [FuelMgr sharedInstance].choiceRecordModel = model;
    __weak __typeof(self) wself = self;
    [[FuelMgr sharedInstance]addNewFuelRecord:model succ:^{
        [[StatisticsMgr sharedInstance] addClickID:@"加油记录-保存新增加油记录按钮" description:@""];
        [AlertMgr showShortSuccessTextTips:@[@"保存数据成功"]];
        [wself dismissViewControllerAnimated:YES completion:nil];
        
    } fail:^{
        
    }];
}

- (void)createTableView
{
    self.tableView.frame = CGRectMake(0, 0 , __MAIN_WIDTH , __MAIN_HEIGHT_64 );
    self.tableView.backgroundColor = [Theme backGroundColor];
    self.tableView.separatorColor = [Theme lineNewColor];
    self.tableView.bounces = (__MAIN_HEIGHT == 480 ? YES : NO);
    self.tableView.tableFooterView = [[UIView alloc]init];

}
- (NSArray *)titleArray
{
    return @[@[@"空白"],@[@[@"加油金额",@"(元)"],@[@"汽油标号",@""],@[@"汽油单价",@"(元)"],@[@"加油升数",@"(升)"]],@[@"空白"],@[@[@"加时里程",@"(公里)"],@[@"加油时间",@""],@[@"加油站",@""]],@[@"删除记录"]];
}
#pragma mark - tableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.titleArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.titleArray[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return ((indexPath.section == 2 || indexPath.section == 0) ? (((self.vcType == VC_Type_CanEdit_Add || self.vcType == VC_Type_CanEdit_NoMap ) && indexPath.section == 2) ? SECTION_HEIGHT_WORD : SECTION_HEIGHT) : (indexPath.section == 4 ? (((self.vcType == VC_Type_CanEdit_Map) || (self.vcType == VC_Type_CanEdit_NoMap)) ? CELL_HEIGHT_BTN : 0) : ((indexPath.section == 1 && indexPath.row == 0) ? CELL_HEIGHT_BIG : CELL_HEIGHT)));
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell;
    CGFloat labelX = 20.5 * __MAIN_RATIO_375;
    switch (indexPath.section) {
            //常规cell
        case 1:
        case 3:
        {
            NSString * cellID = ((indexPath.section == 1 && indexPath.row == 0 ) ? [NSString stringWithFormat:@"cellBigID%zd%zd",indexPath.section, indexPath.row] : [NSString stringWithFormat:@"cellID%zd%zd",indexPath.section ,indexPath.row]);
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                UILabel * leftLabel = [Factory labelWithFrame:CGRectMake(labelX, 0, 115, (indexPath.section == 1 && indexPath.row == 0) ? CELL_HEIGHT_BIG : CELL_HEIGHT) font:FONT_DEFAULT_Light(15) text:@"" textColor:(indexPath.section == 1 ? [Theme wordColor] : COLOR_HEXSTRING(@"#bdbdbd")) onView:cell textAlignment:NSTextAlignmentLeft];
                leftLabel.attributedText = [self titleWithNameStr:self.titleArray[indexPath.section][indexPath.row][0] withUnitStr:self.titleArray[indexPath.section][indexPath.row][1] withUnitFontSize:12 withUnitColor:COLOR_HEXSTRING(@"bdbdbd")];
                leftLabel.tag = View_Type_LeftL;
                
                UITextField * textFied = [Factory textFieldWithFrame:CGRectMake(leftLabel.right, 0, __MAIN_WIDTH - labelX - leftLabel.right, leftLabel.height) hideText:NO defaultText:@"" onView:cell];
                [textFied setTextColor:(indexPath.section == 3 ? COLOR_HEXSTRING(@"#bdbdbd") : (indexPath.row == 0 ? COLOR_HEXSTRING(@"#ff3d00") : (indexPath.row == 3 ? COLOR_HEXSTRING(@"#bdbdbd") : [Theme wordColor])))];
                textFied.font = FONT_DEFAULT_Light(15);
                textFied.textAlignment = NSTextAlignmentRight;
                textFied.tag = View_Type_TF;
                textFied.keyboardType = UIKeyboardTypeDecimalPad;
                [self registerTextField:textFied];
                [[[UIView viewWithFrame:CGRectMake(0, leftLabel.bottom - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell];
            }
            UITextField * inputTF = (UITextField *)[cell viewWithTag:View_Type_TF];
            [inputTF setEnabled:((indexPath.section == 1 && (indexPath.row != 3 || indexPath.row != 1)) ? ((self.vcType == VC_Type_CanEdit_NoMap || self.vcType == VC_Type_CanEdit_Map || self.vcType == VC_Type_CanEdit_Add) ? YES : NO) : NO)];
            if(indexPath.section == 1)
            {
                switch (indexPath.row) {
                    case 0://加油金额
                        inputTF.placeholder = @"请输入加油金额";
                        self.fuelCountPriceTF = inputTF;
                        [self.fuelCountPriceTF setFont:FONT_DEFAULT_Light([self.fuelCountPriceTF.text isNotNil] ? 25 : 15)];
                        if(self.model.modelDic > 0) inputTF.text = [NSString stringWithFormat:@"%.02f",self.model.totalPrice];
                        [self.fuelCountPriceTF setFont:FONT_DEFAULT_Light([self.fuelCountPriceTF.text isNotNil] ? 25 : 15)];
                       
                        break;
                    case 1://汽油标号
                        inputTF.enabled = NO;
                        inputTF.placeholder = @"请选择汽油标号";
                        inputTF.text = [FuelMgr sharedInstance].choiceFuelPriceModel.showFuelLabel;
                        self.fuelNumTF = inputTF;
                        if(self.model.modelDic > 0) inputTF.text = self.model.NEWFUELLABEL;
                        break;
                    case 2://汽油单价
                        inputTF.placeholder = @"请输入汽油单价";
                        self.fuelUnitPriceTF = inputTF;
                        inputTF.text = [NSString stringWithFormat:@"%.02f",[FuelMgr sharedInstance].choiceFuelPriceModel.price];
                        if(self.model.modelDic > 0) inputTF.text = [NSString stringWithFormat:@"%.02f",self.model.FUELPRICE];
                        break;
                    case 3://加油升数
                        if(self.vcType == VC_Type_Look)[inputTF setTextColor:[Theme wordColor]];
                        inputTF.enabled = NO;
                        inputTF.placeholder = @"暂无数据";
                        self.fuelOilTF = inputTF;
                        if(self.model.modelDic > 0) inputTF.text = [NSString stringWithFormat:@"%.01f",self.model.fuelVol];
                        break;
                    default:
                        break;
                }

            }else if(indexPath.section == 3)
            {
                UILabel * leftL = (UILabel *)[cell viewWithTag:View_Type_LeftL];
                [leftL setTextColor:((self.vcType == VC_Type_CanEdit_Map) ?  COLOR_HEXSTRING(@"#bdbdbd") : [Theme wordColor]) ];
                [inputTF setTextColor:((self.vcType == VC_Type_CanEdit_Map) ?  COLOR_HEXSTRING(@"#bdbdbd") : [Theme wordColor])];
                inputTF.enabled = ((self.vcType == VC_Type_CanEdit_Map || self.vcType == VC_Type_Look) ? NO : YES);
                switch (indexPath.row) {
                    case 0://加时里程
                        inputTF.placeholder = @"未输入车辆里程";
                        self.fuelTimeMileTF = inputTF;
                        if(self.model.modelDic > 0) inputTF.text = STRING_INT(self.model.NEWRECORDS);
                        break;
                    case 1://加油时间
                    {
                        inputTF.placeholder = @"未输入加油时间";
                        inputTF.enabled = NO;

                        self.fuelTimeTF = inputTF;
                        if(self.vcType == VC_Type_CanEdit_Add)inputTF.text = [CALENDARMGR dateStrWithDate:[NSDate date] format:FORMAT];
                        if(self.model.modelDic > 0) inputTF.text = self.model.cdt;
                    }
                        break;
                    case 2://加油站
                        inputTF.placeholder = @"未指定加油站";
                        inputTF.keyboardType = UIKeyboardTypeWebSearch;
                        self.fuelPlaceTF = inputTF;
                        if(self.model.modelDic > 0) inputTF.text = self.model.GSTATION_NAME;
                        break;
                    default:
                        break;
                }
            }
            
        }
            break;
            
        case 4:
        {
            NSString * cellID = @"btnCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                cell.backgroundColor = [UIColor clearColor];
                cell.backgroundColor = [Theme backGroundColor];
                //删除按钮
                UIButton * deleteBtn = [ViewFactory buttonWithFrame:CGRectMake(0, CELL_HEIGHT_BTN - CELL_HEIGHT, __MAIN_WIDTH, CELL_HEIGHT)  title:self.titleArray[indexPath.section][indexPath.row] normalColor:[UIColor whiteColor] highlightColor:[[UIColor whiteColor] colorWithAlphaComponent:0.7] disabledColor:[Theme wordGrayColor] inView:cell action:nil];
                deleteBtn.layer.borderColor = [Theme lineColor].CGColor;
                deleteBtn.layer.borderWidth = 0.5;
                [deleteBtn setTitleColor:COLOR_HEXSTRING(@"#ff3d00") forState:UIControlStateNormal];
                [deleteBtn.titleLabel setFont:FONT_DEFAULT_Light(16)];
                deleteBtn.layer.cornerRadius = 0;
                deleteBtn.tag = View_Type_Delete_Btn;

                
            }
            UIButton * deleteBtn = (UIButton *)[cell viewWithTag:View_Type_Delete_Btn];
            [deleteBtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
            [deleteBtn setHidden:(((self.vcType == VC_Type_CanEdit_NoMap) || (self.vcType == VC_Type_CanEdit_Map)) ? NO : YES)];
        }
            break;
            //section view cell
        case 2:
        case 0:
        {
            NSString * cellID = (indexPath.section == 0 ? @"noCellID" : @"noCellIDWord");
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                cell.backgroundColor = [Theme backGroundColor];
                [[[UIView viewWithFrame:CGRectMake(0, (((self.vcType == VC_Type_CanEdit_Add || self.vcType == VC_Type_CanEdit_NoMap) && indexPath.section == 2) ? SECTION_HEIGHT_WORD : SECTION_HEIGHT) - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:[Theme lineColor]] setupOnView:cell];
                
                if((self.vcType == VC_Type_CanEdit_Add || self.vcType == VC_Type_CanEdit_NoMap) && indexPath.section == 2) [Factory labelWithFrame:CGRectMake(labelX, SECTION_HEIGHT, __MAIN_WIDTH - labelX * 2, SECTION_HEIGHT_WORD - SECTION_HEIGHT) font:FONT_DEFAULT_Light(13) text:@"完善选填信息，会使数据计算更准确" textColor:COLOR_HEXSTRING(@"#bdbdbd") onView:cell textAlignment:NSTextAlignmentLeft];
            }
        }
            break;
        default:
            break;
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
#pragma makr - 删除按钮
- (void)deleteBtnClick{
    [self clickOnBG];
    __weak __typeof(self) wself = self;
    TipsView *tipV = [TipsView textTipsWithTitle:nil lightTitles:@[@"您确定删除此条加油记录吗?"]];
    [tipV addLeftButton:@"暂不删除" clickLeft:^{
        
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
    } rightButton:@"确认删除" clickRight:^{
        [[FuelMgr sharedInstance] deleteFuelRecord:wself.model succ:^{
            [FuelMgr sharedInstance].choiceRecordModel = nil;
            [AlertMgr showShortSuccessTextTips:@[@"删除数据成功"]];
            [wself dismissViewControllerAnimated:YES completion:nil success:YES info:nil];
        } fail:^{
            [AlertMgr showShortWarnTextTips:@[@"删除数据失败"]];
        }];
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
    }];
    [AlertMgr showView:tipV];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self clickOnBG];
    //更改汽油标号
    if(indexPath.section == 1 && indexPath.row == 1)
    {
        [self clickOnBG];
        if(self.vcType == VC_Type_CanEdit_Add || self.vcType == VC_Type_CanEdit_Map || self.vcType == VC_Type_CanEdit_NoMap)[AlertMgr showPopupView:self.oilPickerBackView bottom:0];
    }
    //更改时间
    else if(indexPath.section == 3 && indexPath.row == 1)
    {
        [self clickOnBG];
        if(self.vcType == VC_Type_CanEdit_Add || self.vcType == VC_Type_CanEdit_NoMap){
            [AlertMgr showPopupView:self.datePickBackView bottom:0];
            if (self.vcType == VC_Type_CanEdit_NoMap) {
                NSArray * dateArr = [self.model.cdt componentsSeparatedByString:@"  "];
                if(dateArr.count > 1)[self.datePickView selectRow:[self.datePickerHournArr indexOfObject:dateArr[1]] inComponent:1 animated:NO];
                [self.datePickView selectRow:[self.pickDates indexOfObject:dateArr[0]] inComponent:0 animated:NO];
                
            }else if(self.vcType == VC_Type_CanEdit_Add)
            {
                NSArray * dateArr = [[CALENDARMGR dateStrWithDate:[NSDate date] format:FORMAT] componentsSeparatedByString:@"  "];
                if(dateArr.count > 1)[self.datePickView selectRow:[self.datePickerHournArr indexOfObject:dateArr[1]] inComponent:1 animated:NO];
                [self.datePickView selectRow:[self.pickDates indexOfObject:dateArr[0]] inComponent:0 animated:NO];
            }
        }
    }else
    {
//            [self.navigationController pushViewController:[[[OilMapDetailViewController alloc] initWithTableRefreshType:TYPE_TABLE_REFRESH_NONE] navSetupBackItemWithIdentification:BACK_ICON_TITLE] animated:YES];
    }
    
}
-(BOOL)textFieldCanAddString:(NSString *)string withTextField:(UITextField *)textField
{
    
    if (textField == self.fuelCountPriceTF || textField == self.fuelUnitPriceTF) {
        NSArray * textArr = [textField.text componentsSeparatedByString:@"."];
        if (textArr.count > 1 ) {
            if ([string isEqualToString:@"."]) {
                return NO;
            }else if([NSString stringWithFormat:@"%@%@",textArr[1],string].length > 2)
            {
                return NO;
            }

        }
        if([[NSString stringWithFormat:@"%@%@",textField.text,string] floatValue] == 0.00 && [[NSString stringWithFormat:@"%@%@",textField.text,string] length] > 3)
        {
            [AlertMgr showShortNormalTextTips:@[(textField == self.fuelCountPriceTF ? @"加油金额不能为0" : @"汽油单价不能为0")]];
            return NO;
        }
        if (([[NSString stringWithFormat:@"%@%@",textField.text,string] integerValue] > 5000 && textField == self.fuelCountPriceTF) || ([[NSString stringWithFormat:@"%@%@",textField.text,string] integerValue] > 1000 && textField == self.fuelUnitPriceTF)) {
            [AlertMgr showShortNormalTextTips:@[@"您输入的金额过大"]];
            return NO;
        }
    }
    return YES;
}
- (void)textFieldTextChanged:(UITextField *)textField
{
    if(textField == self.fuelCountPriceTF)
    {
        [self.fuelCountPriceTF setFont:FONT_DEFAULT_Light(([textField.text isNotNil] && [self.fuelCountPriceTF.text isNotNil]) ? 25 : 15)];
    }
    if([self.fuelCountPriceTF.text isNotNil] && [self.fuelUnitPriceTF.text isNotNil])
    {
        
        self.fuelOilTF.text = ([self.fuelUnitPriceTF.text floatValue] == 0.00 || [self.fuelCountPriceTF.text floatValue] == 0.00) ? @"" :[NSString stringWithFormat:@"%.01f",([self.fuelCountPriceTF.text floatValue] / [self.fuelUnitPriceTF.text floatValue] * 1.00)];
    }else
    {
        self.fuelOilTF.text = @"";
    }
}

-(void)moveViewWhenKeyBoardChangeToHeight:(CGFloat)height WithDuiation:(NSTimeInterval)duration{
    __weak __typeof(self) wself = self;
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
       
        if(height == 0 )
        {
            [wself.tableView setHeight:__MAIN_HEIGHT_64];
        }else
        {
            if(wself.tableView.height == __MAIN_HEIGHT_64) wself.tableView.height = wself.tableView.height - height;
        }
    } completion:nil];
}

#pragma mark - 生成使用的attribute str
- (NSMutableAttributedString *)titleWithNameStr:(NSString *)nameStr withUnitStr:(NSString *)unitStr withUnitFontSize:(NSInteger)unitFontSize withUnitColor:(UIColor *)color
{
    NSString * str = [NSString stringWithFormat:@"%@ %@",nameStr, unitStr];
    NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc]initWithString:str];
    if([unitStr length] > 0)
    {
        NSRange range = NSMakeRange([str length] - [unitStr length], [unitStr length]);
        [attributeStr addAttribute:NSFontAttributeName value:FONT_DEFAULT_Light(unitFontSize) range:range];
        if(color)[attributeStr addAttribute:NSForegroundColorAttributeName value:color range:range];
    }
    return attributeStr;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self clickOnBG];
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    
    if ((![FuelMgr sharedInstance].choiceRecordModel) && self.vcType != VC_Type_CanEdit_Add) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
     if((self.vcType == VC_Type_CanEdit_Add || self.vcType == VC_Type_CanEdit_Map || self.vcType == VC_Type_CanEdit_NoMap)  && self.fuelCountPriceTF)[self.fuelCountPriceTF becomeFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
